package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    //从框架中获取已经登录的用户的用户名,用与回显
    @RequestMapping("/getusername")
    public Result getusername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user!=null){
            //获取当前用户成功
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }else {
            //获取当前用户失败
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }

    }

    //修改密码
    @RequestMapping("/updatepassword")
    public Result updatepassword(@RequestBody Map password){
        System.out.println(password.get("oldcode"));
        try {
            String oldcode = (String) password.get("oldcode");//旧密码
            String newcode = (String) password.get("newcode");//新密码
            Result getusername = getusername();
            String username = (String) getusername.getData(); //当前用户名
            boolean updatepassword = userService.updatepassword(oldcode, newcode, username);
            if(updatepassword){
                //修改成功
                return new Result(true,"修改成功");
            }else {
                //修改失败
                return new Result(false,"旧密码输入错误!请重新输入");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败，服务器异常");
        }
    }

    //判断密码是否正确
    @RequestMapping("/Ackpassword")
    public Result Ackpassword(@RequestBody Map user){
        //判断密码是否正确
        boolean ackpassword = userService.Ackpassword(user);
        if(ackpassword){
            return new Result(true,"密码正确");
        }else {
            return new Result(false, "密码错误");
        }

    }

    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user){
        try {
            userService.add(user);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    //用户页面分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        PageResult pageResult= userService.pageQuery(queryPageBean);
        System.out.println(pageResult);
        return pageResult;
    }


    //编辑用户中查询回显用户信息用
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.itheima.pojo.User user =userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USERID_SUCCESS,user);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_USERID_FAIL);
        }
    }
    //编辑用户中查询角色信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list =userService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //添加角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roles){
        try {
            userService.edit(user,roles);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }

    @RequestMapping("/deleteById")
    public Result DeleteById(Integer id){
        try {
            userService.DeleteById(id);
//            if(b){
//                return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
//            }else {
//                return new Result(false,"删除失败!该用户已跟角色绑定");
//            }
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }


}
